package yxk.task;

import kd.bos.context.RequestContext;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.schedule.executor.AbstractTask;
import kd.sdk.plugin.Plugin;
import kd.wtc.wtis.webapi.punchcard.PunchCardApi;
import kd.wtc.wtis.webapi.punchcard.model.PunchCardReqMode;
import kd.wtc.wtis.webapi.punchcard.model.PunchCardRespModel;
import kd.wtc.wtis.webapi.punchcard.validate.ListValidator;
import yxk.task.utils.PunchcardDataUtil;
import yxk.utils.DateUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 海康打卡数据匹配
 * 同步前一天开始时间到至今
 */
public class PunchcardDataHAIKANTask extends AbstractTask implements Plugin {
    private static final Log LOG = LogFactory.getLog(PunchcardDataHAIKANTask.class);
    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        PunchcardDataUtil punchcardDataUtil=new PunchcardDataUtil();
        PunchCardApi api=new PunchCardApi();
        Date date = DateUtil.MonthAdd(new Date(),Calendar.DATE ,-1);
        //查询的开始时间和结束时间
        String startStr = DateUtil.getISO8601TimestampFromDateStr(date);
        String receiveEndTime = DateUtil.getISO8601TimestampFromDateStr(new Date());

        List<ListValidator<PunchCardReqMode>> haikanPunchcardObtainList = punchcardDataUtil.getHAIKANPunchcardObtainList(startStr,receiveEndTime,startStr,receiveEndTime);
        LOG.info("PunchcardDataHAIKANTask.execute.haikanPunchcardObtainList:{}",haikanPunchcardObtainList);
        for (ListValidator<PunchCardReqMode> punchCardReqModes : haikanPunchcardObtainList) {
            CustomApiResult<PunchCardRespModel> punchCardRespModel = api.syncCardRecordService(punchCardReqModes);
            LOG.info("PunchcardDataHAIKANTask.execute.punchCardRespModel:{}",punchCardRespModel);
        }
    }
}